// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.Lua;

import gnu.trove.list.array.TShortArrayList;
import java.util.ArrayList;
import java.util.HashMap;
import se.krka.kahlua.vm.KahluaTable;
import se.krka.kahlua.vm.KahluaTableIterator;
import se.krka.kahlua.vm.LuaClosure;
import se.krka.kahlua.vm.Prototype;
import zombie.core.Core;
import zombie.core.logger.ExceptionLogger;
import zombie.iso.IsoChunk;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;
import zombie.iso.objects.IsoWorldInventoryObject;
import zombie.network.GameServer;
import zombie.network.ServerMap;

public final class MapObjects {
    private static final HashMap<String, MapObjects.Callback> onNew = new HashMap();
    private static final HashMap<String, MapObjects.Callback> onLoad = new HashMap();
    private static final ArrayList<IsoObject> tempObjects = new ArrayList();
    private static final Object[] params = new Object[1];

    private static MapObjects.Callback getOnNew(String string) {
        MapObjects.Callback callback = (MapObjects.Callback)onNew.get(string);
        if (callback == null) {
            callback = new MapObjects.Callback(string);
            onNew.put(string, callback);
        }

        return callback;
    }

    public static void OnNewWithSprite(String string, LuaClosure luaClosure, int int1) {
        if (string != null && !string.isEmpty()) {
            if (luaClosure == null) {
                throw new NullPointerException("function is null");
            } else {
                MapObjects.Callback callback = getOnNew(string);

                for (int int0 = 0; int0 < callback.functions.size(); int0++) {
                    if (callback.priority.get(int0) < int1) {
                        callback.functions.add(int0, luaClosure);
                        callback.priority.insert(int0, (short)int1);
                        return;
                    }

                    if (callback.priority.get(int0) == int1) {
                        callback.functions.set(int0, luaClosure);
                        callback.priority.set(int0, (short)int1);
                        return;
                    }
                }

                callback.functions.add(luaClosure);
                callback.priority.add((short)int1);
            }
        } else {
            throw new IllegalArgumentException("invalid sprite name");
        }
    }

    public static void OnNewWithSprite(KahluaTable kahluaTable, LuaClosure luaClosure, int _int) {
        if (kahluaTable != null && !kahluaTable.isEmpty()) {
            if (luaClosure == null) {
                throw new NullPointerException("function is null");
            } else {
                KahluaTableIterator kahluaTableIterator = kahluaTable.iterator();

                while (kahluaTableIterator.advance()) {
                    Object object = kahluaTableIterator.getValue();
                    if (!(object instanceof String)) {
                        throw new IllegalArgumentException("expected string but got \"" + object + "\"");
                    }

                    OnNewWithSprite((String)object, luaClosure, _int);
                }
            }
        } else {
            throw new IllegalArgumentException("invalid sprite-name table");
        }
    }

    public static void newGridSquare(IsoGridSquare square) {
        if (square != null && !square.getObjects().isEmpty()) {
            tempObjects.clear();

            for (int int0 = 0; int0 < square.getObjects().size(); int0++) {
                tempObjects.add(square.getObjects().get(int0));
            }

            for (int int1 = 0; int1 < tempObjects.size(); int1++) {
                IsoObject isoObject = (IsoObject)tempObjects.get(int1);
                if (square.getObjects().contains(isoObject) && !(isoObject instanceof IsoWorldInventoryObject) && isoObject != null && isoObject.sprite != null
                    )
                 {
                    String string = isoObject.sprite.name == null ? isoObject.spriteName : isoObject.sprite.name;
                    if (string != null && !string.isEmpty()) {
                        MapObjects.Callback callback = (MapObjects.Callback)onNew.get(string);
                        if (callback != null) {
                            params[0] = isoObject;

                            for (int int2 = 0; int2 < callback.functions.size(); int2++) {
                                try {
                                    LuaManager.caller.protectedCallVoid(LuaManager.thread, callback.functions.get(int2), params);
                                } catch (Throwable throwable) {
                                    ExceptionLogger.logException(throwable);
                                }

                                string = isoObject.sprite != null && isoObject.sprite.name != null ? isoObject.sprite.name : isoObject.spriteName;
                                if (!square.getObjects().contains(isoObject) || isoObject.sprite == null || !callback.spriteName.equals(string)) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private static MapObjects.Callback getOnLoad(String string) {
        MapObjects.Callback callback = (MapObjects.Callback)onLoad.get(string);
        if (callback == null) {
            callback = new MapObjects.Callback(string);
            onLoad.put(string, callback);
        }

        return callback;
    }

    public static void OnLoadWithSprite(String string, LuaClosure luaClosure, int int1) {
        if (string != null && !string.isEmpty()) {
            if (luaClosure == null) {
                throw new NullPointerException("function is null");
            } else {
                MapObjects.Callback callback = getOnLoad(string);

                for (int int0 = 0; int0 < callback.functions.size(); int0++) {
                    if (callback.priority.get(int0) < int1) {
                        callback.functions.add(int0, luaClosure);
                        callback.priority.insert(int0, (short)int1);
                        return;
                    }

                    if (callback.priority.get(int0) == int1) {
                        callback.functions.set(int0, luaClosure);
                        callback.priority.set(int0, (short)int1);
                        return;
                    }
                }

                callback.functions.add(luaClosure);
                callback.priority.add((short)int1);
            }
        } else {
            throw new IllegalArgumentException("invalid sprite name");
        }
    }

    public static void OnLoadWithSprite(KahluaTable kahluaTable, LuaClosure luaClosure, int _int) {
        if (kahluaTable != null && !kahluaTable.isEmpty()) {
            if (luaClosure == null) {
                throw new NullPointerException("function is null");
            } else {
                KahluaTableIterator kahluaTableIterator = kahluaTable.iterator();

                while (kahluaTableIterator.advance()) {
                    Object object = kahluaTableIterator.getValue();
                    if (!(object instanceof String)) {
                        throw new IllegalArgumentException("expected string but got \"" + object + "\"");
                    }

                    OnLoadWithSprite((String)object, luaClosure, _int);
                }
            }
        } else {
            throw new IllegalArgumentException("invalid sprite-name table");
        }
    }

    public static void loadGridSquare(IsoGridSquare square) {
        if (square != null && !square.getObjects().isEmpty()) {
            tempObjects.clear();

            for (int int0 = 0; int0 < square.getObjects().size(); int0++) {
                tempObjects.add(square.getObjects().get(int0));
            }

            for (int int1 = 0; int1 < tempObjects.size(); int1++) {
                IsoObject isoObject = (IsoObject)tempObjects.get(int1);
                if (square.getObjects().contains(isoObject) && !(isoObject instanceof IsoWorldInventoryObject) && isoObject != null && isoObject.sprite != null
                    )
                 {
                    String string = isoObject.sprite.name == null ? isoObject.spriteName : isoObject.sprite.name;
                    if (string != null && !string.isEmpty()) {
                        MapObjects.Callback callback = (MapObjects.Callback)onLoad.get(string);
                        if (callback != null) {
                            params[0] = isoObject;

                            for (int int2 = 0; int2 < callback.functions.size(); int2++) {
                                try {
                                    LuaManager.caller.protectedCallVoid(LuaManager.thread, callback.functions.get(int2), params);
                                } catch (Throwable throwable) {
                                    ExceptionLogger.logException(throwable);
                                }

                                string = isoObject.sprite != null && isoObject.sprite.name != null ? isoObject.sprite.name : isoObject.spriteName;
                                if (!square.getObjects().contains(isoObject) || isoObject.sprite == null || !callback.spriteName.equals(string)) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static void debugNewSquare(int x, int y, int z) {
        if (Core.bDebug) {
            IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(x, y, z);
            if (isoGridSquare != null) {
                newGridSquare(isoGridSquare);
            }
        }
    }

    public static void debugLoadSquare(int x, int y, int z) {
        if (Core.bDebug) {
            IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(x, y, z);
            if (isoGridSquare != null) {
                loadGridSquare(isoGridSquare);
            }
        }
    }

    public static void debugLoadChunk(int wx, int wy) {
        if (Core.bDebug) {
            IsoChunk isoChunk = GameServer.bServer ? ServerMap.instance.getChunk(wx, wy) : IsoWorld.instance.CurrentCell.getChunk(wx, wy);
            if (isoChunk != null) {
                for (int int0 = 0; int0 <= isoChunk.maxLevel; int0++) {
                    for (int int1 = 0; int1 < 10; int1++) {
                        for (int int2 = 0; int2 < 10; int2++) {
                            IsoGridSquare isoGridSquare = isoChunk.getGridSquare(int1, int2, int0);
                            if (isoGridSquare != null && !isoGridSquare.getObjects().isEmpty()) {
                                loadGridSquare(isoGridSquare);
                            }
                        }
                    }
                }
            }
        }
    }

    public static void reroute(Prototype prototype, LuaClosure luaClosure1) {
        for (MapObjects.Callback callback : onNew.values()) {
            for (int _int = 0; _int < callback.functions.size(); _int++) {
                LuaClosure luaClosure0 = (LuaClosure)callback.functions.get(_int);
                if (luaClosure0.prototype.filename.equals(prototype.filename) && luaClosure0.prototype.name.equals(prototype.name)) {
                    callback.functions.set(_int, luaClosure1);
                }
            }
        }
    }

    public static void Reset() {
        onNew.clear();
        onLoad.clear();
    }

    private static final class Callback {
        final String spriteName;
        final ArrayList<LuaClosure> functions = new ArrayList();
        final TShortArrayList priority = new TShortArrayList();

        Callback(String string) {
            this.spriteName = string;
        }
    }
}
